

window._loop = false;
window.vertexArray = new Float32Array([
    // 三角形三个顶点坐标的x、y、z值
    0.0, 0.0, 0.0,//顶点1坐标
    1.0, 0.0, 0.0,//顶点2坐标
    0.0, 1.0, 0.0,//顶点3坐标
]);

/**
 * 慎用，该类会导致频闪
 * 
 * 通用渲染辅助类
 */
export default class ChangeBuffer{

    constructor(renderFunction){

        window.changeBuffer = function () {
            _loop = false;
            vertexArray[0] = -1;
            vertexArray[7] = -1;
            renderFunction(vertexArray, { r: 0.5, g: 0.5, b: 0, a: 1.0 });
        }

        window.reset = function () {
            _loop = false;
            vertexArray[0] = 0;
            vertexArray[7] = 1;
            renderFunction(vertexArray);
        }

        function render() {
            if (_loop) {
                requestAnimationFrame(render);
            }
            frameIndex++;
            if (frameIndex % 2 > 0) {
                vertexArray[0] = -1;
                vertexArray[7] = -1;
                renderFunction(vertexArray, { r: 0.5, g: 0.5, b: 0, a: 1.0 });
            }
            else {
                vertexArray[0] = 0;
                vertexArray[7] = 1;
                renderFunction(vertexArray);
            }
        }

        let frameIndex = 0;
        window.loop = function () {
            _loop = true;
            render();
        }
    }
};